using System;
using System.Collections.Generic;
using System.Text;
using openfixfast;

namespace at_csharp
{
    class atApplication : Application
    {

        public override void onCreate(SessionID sessionID)
        {
            System.Console.WriteLine("onCreate" + sessionID.toString());
            Session session = Session.lookupSession(sessionID);
        }
        public override void onLogon(SessionID sessionID)
        {
            System.Console.WriteLine("onLogon" + sessionID.toString());        
        }

        public override void onLogout(SessionID sessionID)
        {
            System.Console.WriteLine("onLogout" + sessionID.toString());
        }
        public override void toApp(Message message, SessionID sessionID)
        {
        }
        public override void fromAdmin(Message msg, SessionID sessionID)
        {
            System.Console.WriteLine("fromAdmin" + sessionID.toString());
        }
        public override void toAdmin(Message msg, SessionID sessionID)
        {
            MsgType msgType = new MsgType();
            msg.getHeader().getField(msgType);
            if ('A' == msgType.getValue().ToCharArray()[0])
            {
                msg.setField(new FixAppVersion("FIXT.1.1"));
                msg.setField(new Username("USER_NAME"));
                msg.setField(new Password("xxxxxxx"));
                msg.setField(1137, "5"); 
            }
            System.Console.WriteLine("toAdmin:" + msg.toString());
        }
        public override void fromApp(Message message, SessionID sessionID)
        {
            System.Console.WriteLine("fromApp" + sessionID.toString());
            MsgType msgType = new MsgType();
            message.getHeader().getField(msgType);
            if (msgType.getValue().ToCharArray()[0] == '0')
            {
                //Heartbeat
            }
            else if (msgType.getValue().ToCharArray()[0] == 'A')
            {
                ////Logon
                ////e.g. howto - create and use group 
                // Session sess = Session.lookupSession(sessionID);
                // Message msg = sess.createMsg("D");
                // msg.setField(new Side(openfixfast_csharp.Side_BUY));
                // msg.setField(new TransactTime(true));
                // msg.setField(new OrdType(openfixfast_csharp.OrdType_LIMIT));
                // msg.setField(new HandlInst('1'));
                // msg.setField(new Symbol("SYMBOL_NAME"));
                // msg.setField(new OrderQty(55));
                // msg.setField(new TimeInForce(openfixfast_csharp.TimeInForce_DAY));
                // msg.setField(new Price(100));
                // NoPartyIDs noPartyIDs = new NoPartyIDs();
                // Group group101 = new Group( 453, 448 ); 
                // Group group36 = new Group( 453, 448 ); 
                // uint num = 1; 
                // msg.getGroup(num, group101);
                // msg.getGroup(num, group36); 
                // group101.setField(448, "USER_NAME"); 
                // group101.setField(452, "101"); 
                // group36.setField(448, "USER_NAME"); 
                // group36.setField(452, "36");
                // int icount = msg.groupCount(noPartyIDs.getField());
                // msg.setField(453, "2");
                // msg.replaceGroup(num, group101);
                // msg.addGroup(group36);
                // icount = msg.groupCount(noPartyIDs.getField()); 
                // string timeStamp = new TransactTime(true).getString(); 
                // timeStamp = timeStamp.Substring(9, timeStamp.Length ); 
                // for ( int i = 0; i < 1; i++ ) 
                // { 
                //     string ss = timeStamp + "-" + i.ToString() ; 
                //     message.setField(new ClOrdID( ss ));
                //     sess.send(message); 
                // }
            }
            else if (msgType.getValue().ToCharArray()[0] == '5')
            {
                //Logout
                System.Threading.Thread.Sleep(20); 
                Session session = Session.lookupSession( sessionID ); 
                //checkout logout message from Factory 
                Message msg = session.createMsg("5");
                string timeStamp = new TransactTime(true).getString();
                timeStamp = timeStamp.Substring(9, timeStamp.Length);
                message.setField(58, "Logging out at:" + timeStamp); 
                message.setField(1409, "0"); 
                session.send(message);
                System.Threading.Thread.Sleep(10);
             }
            if (msgType.getLength() == 1)
            {
                switch (msgType.getValue().ToCharArray()[0])
                {
                    case 'f':
                        //OnSecurityStatus(message, session);
                        break;
                    case 'y':
                        //OnSecurityList(message, session);
                        break;
                    case 'd':
                        //OnSecurityDefinition(message, session);
                        break;
                    case 'Y':
                        //OnMarketDataRequestReject(message, session);
                        break;
                    case 'W':
                        //OnMarketDataSnapshot(message, session);
                        break;
                    case 'X':
                        //OnMarketDataIncrementalRefresh(message, session);
                        break;
                    default:
                        System.Console.WriteLine(message.toString());
                        break;
                }
            }
            else
            {
                if (msgType.getValue() == "AE")
                {
                    //OnTradeCapture(message, session);
                }
            }
        }
      }
}
